package com.lagou.no2

import java.io.{BufferedReader, InputStreamReader}





object Test {
    //获取控制台输入
    val br = new BufferedReader(new InputStreamReader(System.in));
    //定义石头剪刀布集合
    val fistList = List("石头","剪刀","布")
    //定义电脑角色集合
    var list = List("刘备","关羽","张飞")
    println("*" * 15 + "\n" + " 猜拳游戏开始" +  "\n" + "*" * 15 )
    def main(args: Array[String]): Unit = {
        //while结束开关
        var flag = true
        println("请选择对战的对象 1.刘备 2.关羽 3.张飞")
        //获取控制台用户输入的角色数字
        var role = br.readLine()
        //去数组中拿到角色的名字
        var computer_name =  list(role.toInt - 1)
        println(s"您选择了与${computer_name}对战")
        //while结束开关
        var flag1 = true

        while (flag1){
            //询问是否开始对战
            println("是否开始对战？y/n")
            var consire = br.readLine()
            if("y".equals(consire)){
                //当确认开始对战时，跳出循环
                flag1=false
            }
        }
        //定义用户类
        var user = new User
        //用户名为游客
        user.name = "游客"
        //定义机器人类
        var computer = new Computer
        //机器人名字为用户选择的人物名字
        computer.name = computer_name
        //新建游戏类
        var game = new Game
        while (flag) {
            println("请出拳：1.石头 2.剪刀 3.布")
            //获取用户出的拳
            var userFist = fistList(br.readLine().toInt - 1)
            println(s"你出${userFist}")
            //调用机器人随机出拳
            var computerFist = computer.randFist()
            println(s"${computer_name}出${computerFist}")
            //判断胜负
            jungle(userFist,computerFist,user,computer)
            //询问是否下一轮，如果是的话，继续执行该循环体内容，否的话跳出循环
            println("是否开始下一轮？ y/n")
            var ifNext = br.readLine()
            if(!"y".equals(ifNext)){
                flag = false
                println("退出游戏")
                //跳出循环前，将用户和机器的信息赋值给游戏对象中的对应属性
                game.user = user
                game.computer = computer
            }
        }
        //跳出循环后调用game的展示方法，展示最终结果
        game.show()
    }

    def jungle(userFist:String,computerFist: String,user: User,computer: Computer): Unit ={
        if(userFist.equals(computerFist)){
            println("结果是平局，下次继续努力")
            user.score += 1
            computer.score += 1
            user.peace += 1
            computer.peace += 1

        } else if(("石头".equals(userFist) && "剪刀".equals(computerFist)) || ("剪刀".equals(userFist) && "布".equals(computerFist)) || ("布".equals(userFist) && "石头".equals(computerFist))){
            println("结果是你赢了")
            user.score += 2
            user.win += 1
            computer.loss += 1
        }else{
            println("结果是你输了")
            computer.score += 2
            user.loss += 1
            computer.win += 1
        }
    }
}
